﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TaoPortal.BusinessLogic.Extensions
{
    public static class PageLinqExtensions
    {
        /// <summary>
        /// 生成分页列表
        /// </summary>
        /// <typeparam name="T">列表项类</typeparam>
        /// <param name="allItems">所有列表项</param>
        /// <param name="pageIndex">第几页,默认0开始为第一页</param>
        /// <param name="pageSize">页面个数</param>
        /// <returns>分页列表</returns>
        public static PagedList<T> ToPagedList<T>
            (
                this IQueryable<T> allItems,
                int pageIndex,
                int pageSize
            )
        {
            //if (pageIndex < 1)
            //    pageIndex = 1;
            var itemIndex = pageIndex * pageSize;
            var pageOfItems = allItems.Skip(itemIndex).Take(pageSize);
            var totalItemCount = allItems.Count();
            return new PagedList<T>(pageOfItems, pageIndex, pageSize, totalItemCount);
        }
    }
}
